﻿using IFRS.DataAccess.DAL;
using IFRS.Models;
using IFRS.Web;
using IFRS_Global;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Threading;
using System.Web.UI;

public partial class IFRS_WIN01_4A : PageBase
{
    #region "---------- Events -----------"

    protected void Page_Load(object sender, EventArgs e)
    {
        Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
        if (!(Page.IsPostBack || Page.IsCallback))
        {
            
            this.AddClientScript();
            this.ViewMenu();           
            this.SetErrorMessage();
            this.BindDDL();
            this.SetDefaultData();
            if (Request.QueryString["id"] != null)
            {
                this.BindData(Convert.ToInt32(Request.QueryString["id"].ToString()));
            }
        }
        else
            ClientScript.RegisterHiddenField("isPostBack", "1");
    }

    protected void btnSAVE_Click(object sender, EventArgs e)
    {
        SaveData(false);
    }
    protected void btnSAVE_NEW_Click(object sender, EventArgs e)
    {
        SaveData(true);
    }
    protected void btnCANCEL_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/IFRS/WIN01_1S.aspx");
    }
    protected void btnCLEAR_Click(object sender, EventArgs e)
    {
        Response.Redirect(ResolveUrl(Request.Url.AbsolutePath));
    }
    #endregion "---------- Events -----------"

    #region "----------  Methods -----------"

    private void AddClientScript()
    {
        btnCANCEL.Attributes["OnClick"] = "if(!confirm(\"" + Resources.ifrsResource.ConfirmCancel + "\")) return false;";
        btnCostCenter.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/CostCenter.aspx") + "','700','550');");
        btnCurrencyCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/Currency.aspx") + "','700','550');");
        btnGLAccountCode.Attributes.Add("onClick", "return popWindow('" + ResolveUrl("~/PopupPages/GLAccount.aspx") + "','700','550');");

        txtCostCenter.AddControl(hidCostCenter.ClientID);
        txtCostCenter.AddControl(txtCostCenter.ClientID);
        txtCostCenter.ControlSearch = txtCostCenter.ClientID;
        txtCostCenter.SearchKey = "VWCostCenter_oCOST_CENTER_CD_oCOST_CENTER_NM";

        txtGLAccountCode.AddControl(hidGLAccountCode.ClientID);
        txtGLAccountCode.AddControl(txtGLAccountCode.ClientID);
        txtGLAccountCode.ControlSearch = txtGLAccountCode.ClientID;
        txtGLAccountCode.SearchKey = "GLAccount_oGLAccountCode_oGLAccountName";

        txtCurrencyCode.AddControl(hidCurrencyCode.ClientID);
        txtCurrencyCode.AddControl(txtCurrencyCode.ClientID);
        txtCurrencyCode.ControlSearch = txtCurrencyCode.ClientID;
        txtCurrencyCode.SearchKey = "Currency_oCurrencyCode_oCurrencyName";
    }

    private void ViewMenu()
    {
        MenuTitleBar = Resources.ifrsMenu.WIN01_4A;
    }

    private void SetErrorMessage()
    {
        btnSAVE.ValidationGroup = "Master";
        btnSAVE_NEW.ValidationGroup = "Master";
        reqDataType.SetRequiredField(lblDataType, ddlDataType,btnSAVE);
        reqTransactionDate.SetRequiredField(lblTransactionDate, txtTransactionDate, btnSAVE);
        reqAccountNo.SetRequiredField(lblAccountNo, txtAccountNo, btnSAVE);
        reqCostCenter.SetRequiredField(lblCostCenter, txtCostCenter, btnSAVE);
        reqGLAccountCode.SetRequiredField(lblGLAccountCode, txtGLAccountCode, btnSAVE);
        reqCurrencyCode.SetRequiredField(lblCurrencyCode, txtCurrencyCode, btnSAVE);
        reqStartDepositDate.SetRequiredField(lblStartDeposit, txtStartDepositDate, btnSAVE);
        reqExpiryDate.SetRequiredField(lblExpiryDate, txtExpiryDate, btnSAVE);
        reqAmount.SetRequiredField(lblAmount, txtAmount, btnSAVE);
    }

    private void SetDefaultData()
    {
         txtCurrencyCode.Text = "THB";
         hidCurrencyCode.Value = "THB";       
    }

    private void BindData(int ID)
    {
        List<IslamicDeposit> islamicDeposit = new List<IslamicDeposit>();
        DataTable dt = new SPR_ISALAMICDEPOSIT().SEARCH_BYID(ID);
        if (dt.Rows.Count > 0)
        {
            ConvertHelper.ConvertDataTableToObject(dt, islamicDeposit);
            ddlDataType.SelectedValue = islamicDeposit[0].DataTypeID.ToString();
            txtAccountNo.Text = islamicDeposit[0].AccountNo;
            txtGLAccountCode.Text = islamicDeposit[0].GLAccountCode;
            txtStartDepositDate.Text = islamicDeposit[0].StartDeposit.DateToString();
            txtAmount.Text = islamicDeposit[0].DepositAmount.ToString();
            txtTransactionDate.Text = DateTime.Now.DateToString();
            txtCostCenter.Text = islamicDeposit[0].CostCenterName;
            txtCurrencyCode.Text = islamicDeposit[0].CurrencyCode;
            txtExpiryDate.Text = islamicDeposit[0].ExpiryDate.DateToString();
            txtStatus.Text = "สร้างรายการ";
            hidCurrencyCode.Value = islamicDeposit[0].CurrencyCode;
            hidCostCenter.Value = islamicDeposit[0].CostCenter;
            hidGLAccountCode.Value = islamicDeposit[0].GLAccountCode;
        }
    }

    private void SaveData(bool SaveNew)
    {
        try
        {
            if (txtTransactionDate.Text.AsDate() > txtExpiryDate.Text.AsDate())
            {
                Utility.AlertMessage(Page, string.Format(Resources.ifrsResource.wrnCheckFromTo_2, lblExpiryDate.Text, lblTransactionDate.Text));
                return;
            }

            IslamicDeposit islamicDeposit = new IslamicDeposit();
            islamicDeposit.DataTypeID = ddlDataType.SelectedValue.AsInt32();
            islamicDeposit.AccountNo = txtAccountNo.Text;
            islamicDeposit.GLAccountCode = hidGLAccountCode.Value.ToString();
            islamicDeposit.StartDeposit = txtStartDepositDate.Text.StringToDateAllowNull();
            islamicDeposit.DepositAmount = Convert.ToDecimal(txtAmount.Text);
            islamicDeposit.TransactionDate = txtTransactionDate.Text.StringToDateAllowNull();
            islamicDeposit.CostCenter = hidCostCenter.Value.ToString();
            islamicDeposit.CurrencyCode = hidCurrencyCode.Value.ToString();
            islamicDeposit.ExpiryDate = txtExpiryDate.Text.StringToDateAllowNull();
            islamicDeposit.IsConfirmed = false;
            //islamicDeposit.CreatedDate = DateTime.Now;
            //islamicDeposit.UpdatedDate = DateTime.Now;

            if (ddlDataType.SelectedValue == "1")
            {
                islamicDeposit.RP_GSB_PRDCD = "FCD";
                islamicDeposit.RP_MAPPING_CD = "INV_ISLDP_P";
                islamicDeposit.RP_SOURCE_FILE = "MANUAL_INV_ISLDP";
            }
            else
            {
                islamicDeposit.RP_GSB_PRDCD = "INV_ISLDP";
                islamicDeposit.RP_MAPPING_CD = "INV_ISLDP_D";
                islamicDeposit.RP_SOURCE_FILE = "MANUAL_INV_ISLDP";
            }         
            islamicDeposit.FeeAmount = 0;
            islamicDeposit.ExpenseAmount = 0;
            islamicDeposit.SetStandardField();
            int iID = new SPR_ISALAMICDEPOSIT().INSERT(islamicDeposit);

            if (iID > 0)
            {
                if (SaveNew)
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(Request.Url.AbsolutePath));
                }
                else 
                {
                    this.JS_Alert(Resources.ifrsResource.SaveData, ResolveUrl(string.Format("~/IFRS/WIN01_2E.aspx?&id={0}", iID)));
                }
                
            }
            else
            {
                if (iID == -1)
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.Duplicate);
                }
                else
                {
                    Utility.AlertMessage(Page, Resources.ifrsResource.CanNotSaveData);
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void BindDDL()
    {
        ddlDataType.SetData(DDL_TYPE.IsalamicDeposit, "", false, true);
        txtStatus.Text = "สร้างรายการ";
    }


    #endregion "---------- Methods -----------"
  
}